package face.array;

/**
 * @author spf
 * @date 2024/8/21
 * @time 16:25
 */

/**
 * 生成矩阵
 */
public class GenerateMatrix {

    public static void main(String[] args) {

    }

    private int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int left = 0, top = 0;
        int right = n - 1, bottom = n - 1;
        int target = n * n;
        int num = 1;
        while (num <= target) {
            for (int i = left; i <= right; i++) {
                matrix[top][i] = num++;
            }
            top++;
            for (int i = top; i <= bottom; i++) {
                matrix[i][right] = num++;
            }
            right--;
            for (int i = right; i >= left; i--) {
                matrix[bottom][i] = num++;
            }
            bottom--;
            for (int i = bottom; i >= top; i--) {
                matrix[i][left] = num++;
            }
            left++;
        }
        return matrix;
    }

}
